package com.heu.blood.analysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.analysis.entity.BloodSampleInfoTransfusionEntity;
import com.heu.blood.analysis.vo.BloodSampleInpatientlabReportVo;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.analysis.dao.BloodSampleInfoInpatientlabDao;
import com.heu.blood.analysis.entity.BloodSampleInfoInpatientlabEntity;
import com.heu.blood.analysis.service.BloodSampleInfoInpatientlabService;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("bloodSampleInfoInpatientlabService")
public class BloodSampleInfoInpatientlabServiceImpl extends ServiceImpl<BloodSampleInfoInpatientlabDao, BloodSampleInfoInpatientlabEntity> implements BloodSampleInfoInpatientlabService {
    @Autowired
    BloodSampleInfoInpatientlabDao bloodSampleInfoInpatientlabDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        用于接收前端传过来的参数
        BloodSampleInfoInpatientlabEntity bloodSampleInfoInpatientlabEntity = new BloodSampleInfoInpatientlabEntity();
//        if (Strings.isNotBlank((String) params.get("transfusionId"))){
//            bloodSampleInfoInpatientlabEntity.setTransfusionId((String) params.get("transfusionId")); // 添加transfusionId
//        }
        if (Strings.isNotBlank((String) params.get("patientId"))){
            bloodSampleInfoInpatientlabEntity.setPatientId((String) params.get("patientId")); // 添加patientId
        }
        if (Strings.isNotBlank((String) params.get("aboBloodType"))){
            bloodSampleInfoInpatientlabEntity.setAboBloodType((String) params.get("aboBloodType")); // 添加aboBloodType
        }
        if (Strings.isNotBlank((String) params.get("rhBloodType"))){
            bloodSampleInfoInpatientlabEntity.setRhBloodType((String) params.get("rhBloodType")); // 添加rhBloodType
        }
        if(Strings.isNotBlank((String) params.get("patientName")))
            bloodSampleInfoInpatientlabEntity.setPatientName((String)params.get("patientName"));
        if(Strings.isNotBlank((String) params.get("collector")))
            bloodSampleInfoInpatientlabEntity.setCollector((String)params.get("collector"));
        if(Strings.isNotBlank((String) params.get("recipient")))
            bloodSampleInfoInpatientlabEntity.setRecipient((String)params.get("recipient"));

        String collectStartTime = null;
        String collectEndTime = null;
        if(Strings.isNotBlank((String)params.get("collectStartTime")))
            collectStartTime = (String)params.get("collectStartTime");
        if(Strings.isNotBlank((String)params.get("collectEndTime")))
            collectEndTime = (String)params.get("collectEndTime");
        String receivedStartTime = null;
        String receivedEndTime = null;
        if(Strings.isNotBlank((String)params.get("receivedStartTime")))
            receivedStartTime = (String)params.get("receivedStartTime");
        if(Strings.isNotBlank((String)params.get("receivedEndTime")))
            receivedEndTime = (String)params.get("receivedEndTime");


        LambdaQueryWrapper<BloodSampleInfoInpatientlabEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getTransfusionId()),
                BloodSampleInfoInpatientlabEntity::getTransfusionId, bloodSampleInfoInpatientlabEntity.getTransfusionId())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getPatientId()),
                        BloodSampleInfoInpatientlabEntity::getPatientId, bloodSampleInfoInpatientlabEntity.getPatientId())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getAboBloodType()),
                        BloodSampleInfoInpatientlabEntity::getAboBloodType, bloodSampleInfoInpatientlabEntity.getAboBloodType())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getRhBloodType()),
                        BloodSampleInfoInpatientlabEntity::getRhBloodType, bloodSampleInfoInpatientlabEntity.getRhBloodType())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getPatientName()),
                        BloodSampleInfoInpatientlabEntity::getPatientName, bloodSampleInfoInpatientlabEntity.getPatientName())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getCollector()),
                        BloodSampleInfoInpatientlabEntity::getCollector, bloodSampleInfoInpatientlabEntity.getCollector())
                .eq(Strings.isNotBlank(bloodSampleInfoInpatientlabEntity.getRecipient()),
                        BloodSampleInfoInpatientlabEntity::getRecipient, bloodSampleInfoInpatientlabEntity.getRecipient())
                .ge(Strings.isNotBlank(collectStartTime),BloodSampleInfoInpatientlabEntity::getCollectTime,collectStartTime)
                .le(Strings.isNotBlank(collectEndTime),BloodSampleInfoInpatientlabEntity::getCollectTime,collectEndTime)
                .ge(Strings.isNotBlank(receivedStartTime), BloodSampleInfoInpatientlabEntity::getReceivedTime,receivedStartTime)
                .le(Strings.isNotBlank(receivedEndTime), BloodSampleInfoInpatientlabEntity::getReceivedTime,receivedEndTime)
                .eq(BloodSampleInfoInpatientlabEntity::getIfShow, IF_SHOW.code());

        queryWrapper.orderByDesc(BloodSampleInfoInpatientlabEntity::getGmtModified);
        IPage<BloodSampleInfoInpatientlabEntity> page = this.page(
                new Query<BloodSampleInfoInpatientlabEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public BloodSampleInpatientlabReportVo selectSampleReport(String bloodSampleId) {
        return bloodSampleInfoInpatientlabDao.selectSampleInpatientReport(bloodSampleId);
    }
}